﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using YY.Admin.Core;
using YY.Admin.Services.Service;

namespace YY.Admin.Views.Control
{
    /// <summary>
    /// PaginationDataGridControl.xaml 的交互逻辑
    /// </summary>
    public partial class PaginationDataGridControl : UserControl
    {
        public PaginationDataGridControl()
        {
            InitializeComponent();
           this.Loaded += PaginationDataGridControl_Loaded;
        }

        // 确保用户在控制加载时触发列生成
        private void PaginationDataGridControl_Loaded(object sender, RoutedEventArgs e)
        {
            // 确保 DataGrid 已完全加载
            Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
            {
                // 确保 DataGrid 控件已初始化并且 ItemsSource 不为 null
                var data = DataGrid.ItemsSource;
                if (data == null)
                {
                    return;
                }

                // 获取数据模型的类型
                var type = data.GetType().GetGenericArguments().FirstOrDefault();
                if (type == null) return;

                // 获取所有带有 BindDescriptionAttribute 的属性
                var properties = type.GetProperties()
                                     .Where(prop => Attribute.IsDefined(prop, typeof(BindDescriptionAttribute)))
                                     .OrderBy(prop => ((BindDescriptionAttribute)prop.GetCustomAttribute(typeof(BindDescriptionAttribute))).DisplayIndex)
                                     .ToList();

                foreach (var prop in properties)
                {
                    var attribute = prop.GetCustomAttribute<BindDescriptionAttribute>();
                    if (attribute != null)
                    {
                        // 检查列是否已经存在
                        if (!ColumnExists(attribute.HeaderName))
                        {
                            var column = CreateDataGridColumn(attribute, prop);
                            if (column != null)
                            {
                                // 通过绑定添加列
                                DataGrid.Columns.Add(column);
                            }
                        }
                    }
                }
            }));
        }

        // 检查列是否已经存在
        private bool ColumnExists(string headerName)
        {
            return DataGrid.Columns.Any(c => c.Header.ToString() == headerName);
        }

        private DataGridColumn CreateDataGridColumn(BindDescriptionAttribute attribute, PropertyInfo property)
        {
            DataGridColumn column = null;

            // 根据属性的显示方式来创建不同的列
            switch (attribute.ShowAs)
            {
                case ShowScheme.普通文本:
                    column = new DataGridTextColumn
                    {
                        Header = attribute.HeaderName,
                        Binding = new Binding(property.Name),
                        Width = attribute.Width,
                        DisplayIndex = attribute.DisplayIndex
                    };
                    break;

                case ShowScheme.自定义:
                    // 如果需要自定义列，您可以在这里扩展并处理自定义类型的列
                    break;
            }

            return column;
        }
    }
}
